
import socks, requests
import socket
from concurrent.futures import ThreadPoolExecutor
from irc.bot import SingleServerIRCBot

import irc.client
from threading import local, Thread
import secrets, string, time

def randomString(string_length):
        input = string.ascii_lowercase + string.digits
        return ''.join(secrets.choice(input) for i in range(string_length))

class ProxySocket:
    def __init__(self, ip, port, user_, pass_):
        socket.setdefaulttimeout(300)
        socks.set_default_proxy(socks.SOCKS5, ip, port, username=user_, password=pass_)
        socks.wrap_module(irc.client)

class TestBot(SingleServerIRCBot):
    def __init__(self, server_list, nickname, realname):
        super().__init__(server_list, nickname, realname)

    # def on_privmsg(self, connection, event):
    #     print(event)
    def on_welcome(self, connection, event):
        print(event.arguments)
        print("Connected")
        
        # connection.join("#lmao")
        connection.join("#hebits.support")

        for x in range(5):
            connection.privmsg_many(["HeBoT", "SithLord", "lolol", "RL309AFK", "Mongol"], "מנהל")
        # connection.disconnect()
        self.die()


class ProxyBot:
    def __init__(self, proxy, server_ip, server_port, nickname, realname):
        self.proxy = ProxySocket(*proxy)
        
        self.server_ip = server_ip
        self.server_port = server_port
        self.nickname = nickname
        self.realname = realname

        # TestBot((self.server_ip, self.server_port), self.nickname, self.realname).start()
        bot = TestBot([(self.server_ip, self.server_port)], self.nickname, self.realname)
        bot.start()

        # bot = TestBot(self.server_ip, self.server_port, self.nickname, self.realname)
        # bot.start()
    def start(self):
        # None
        print(self.proxy, self.server_ip, self.server_port, self.nickname, self.realname)
        while True:
            try:
                bot = TestBot(self.proxy, self.server_ip, self.server_port, self.nickname, self.realname)
                bot.start()
                break
            except:
                print("Error creating or running the bot. Retrying...")
                self.proxy = ProxySocket(*proxy)  # Reconnect the proxy



##WORKS BUT UNUSEDget_proxies = requests.get("http://list.rola.vip:8088/user_get_ip_list?token=****&qty=42&time=5&country=ir&format=txt&protocol=socks5&filter=0").text
# with open("proxies.txt", 'r') as f:
#     get_proxies = f.read()
###proxies = get_proxies.split("\n"); del get_proxies

def do_bot():
    irc_server, irc_port = "irc.hebits.net", 6667
    # irc_server, irc_port = "irc.freenode.net", 6667

    # prox, port = secrets.choice(proxies).split(":")
    # proxy = (prox, int(port), None, None)
    proxy = (socket.gethostbyname("geo.iproyal.com"), int(32325), "","lo_session-"+randomString(5))
    # proxy = (socket.gethostbyname("geo.iproyal.com"), int(32325), "*o","l*o_country-ir_session-"+randomString(5)+"")

    nickname, realname = randomString(5)+"", randomString(5)

    bot = ProxyBot(
        proxy, irc_server, irc_port, nickname, realname)
    bot.start()

def do_thread():
    while True:
        try:
            a = do_bot()
            # del a
        except:
            raise

print("hey")
for x in range(3):
    Thread(target=do_thread).start()

##_UnbanTOT900TWO

# with ThreadPoolExecutor(max_workers=20) as e:
#     e.map(do_bot, range(100))